<?php

namespace App\Admin\Controllers;

use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Model;
use App\Admin\Extensions\ExcelExpoter;
use Excel;
use DB;
class ZhenSystemController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('新意甄选-客户信息')
            ->description('客户信息')
            ->body($this->grid());
    }

     /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new User);
        $grid->model()->where('type', 3);
        $grid->filter(function ($filter) {
            $filter->like('customer_name', '客户名称');
            $filter->like('customer_number', '客户号');
            $filter->like('host_mechanism_name', '主办机构名称');
            $filter->between('updated_at', '兑奖时间')->datetime();
            $filter->disableIdFilter();
        });
        $grid->id('ID');
        $grid->customer_name('客户名称');
        $grid->customer_number('客户号');
        $grid->host_mechanism_name('主办机构名称');
        $grid->name('收件人');
        $grid->phone('收件人手机号');
        $grid->address('收件人地址');
        $grid->updated_at('兑奖时间');
//        $grid->disableExport();
        $grid->exporter(new ExcelExpoter());
        $grid->disableCreateButton();
        $grid->actions(function ($actions) {
            $actions->disableEdit();
            $actions->disableDelete();
            $actions->disableView();
        });
        return $grid;
    }

    public function zhenImport(Request $request)
    {

        $filePath = $_FILES['excel']['tmp_name'];
        $table = $this->load_excel($filePath);//调用load_excel方法导入文件
        unset($table[0]);
        foreach ($table as $key => $value) {
            $user = User::where('customer_name', $value[10])->where('customer_number', $value[9])->where('type', 3)->first();

            if (!$user) {
                DB::table('users')->insertGetId([
                    'type' => 3,
                    'performance_branch' => $value[0],
                    'host_branch' => $value[1],
                    'host_mechanism_number' => $value[2],
                    'host_mechanism_name' => $value[3],
                    'host_account_manager_number' => $value[4],
                    'host_account_manager_name' => $value[5],
                    'co_sponsor_account_manager' => $value[6],
                    'is_satisfy_satisfy' => $value[7],
                    'proportion_of_customers' => $value[8],
                    'company_business_credit_customers' => $value[11],
                    'customer_account_opening_date' => $value[12],
                    'cost_customer' => $value[13],
                    'cost_customer_grading' => $value[14],
                    'not_valuable' => $value[15],
                    'active_period_company' => $value[16],
                    'customer_name' => $value[10],
                    'customer_number' => $value[9],
                    'created_at' => date('Y-m-d H:i:s',time())
                ]);
            }

        }

        admin_toastr('新意甄选-信息导入成功','success');
        return redirect(admin_url('zhen-user_excel'));
    }

    private function load_excel($filePath)
    {
        $reader = Excel::load($filePath);//要开始导入文件，可以使用->load($filename)。回调是可选的。
        $reader = $reader->getSheet(0);//得到Excel的第一页内容，如下图3
        return $reader->toArray();
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User);

        return $form;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {


        if ($this->form()->destroy($id)) {
            $data = [
                'status'  => true,
                'message' => trans('admin.delete_succeeded'),
            ];
        } else {
            $data = [
                'status'  => false,
                'message' => trans('admin.delete_failed'),
            ];
        }
        return response()->json($data);
    }
}
